﻿using System;
using Coffee.Configuration;
using System.Reflection;

namespace Coffee.ServiceLocators
{
    public delegate object ValueParserDelegate(string value);

    class ValueParserService : ServiceLocator<string, ValueParserDelegate>
    {
        public static void Initialize()
        {
            ProvidersSection config = ProvidersSection.GetConfiguration("Coffee/ParserProviders");
            if (config != null)
            {
                foreach (KeyTypeMethodElement item in config.Providers)
                {
                    Type t = Type.GetType(item.Type);
                    MethodInfo mi = t.GetMethod(item.Method);
                    AddProvider(item.Key, (ValueParserDelegate)Delegate.CreateDelegate(typeof(ValueParserDelegate), mi));
                }
            }
        }
    }
}
